package com.cdshop.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.cdshop.comm.ApplicationException;
import com.cdshop.dao.obj.CartGoods;
import com.cdshop.entity.Cart;
import com.cdshop.jdbc.PStatementCB2;
import com.cdshop.jdbc.ResultSetCB;
import com.cdshop.web.PageUtil;

public final class CartDAO extends AbstractDAO<Cart> {

	protected CartDAO() {
	}

	protected static CartDAO singleton = new CartDAO();

	public static CartDAO instance() {
		return singleton;
	}
	static class Abc{}
	static final String Create_Cart_Query = "INSERT INTO Cart(uid,gid,price,qty,tstamp) VALUES(?,?,?,?,?)";
	static final String List_Cart_Query = "SELECT c.id AS cid, g.* FROM Cart c INNER JOIN Goods g ON c.gid=g.id WHERE c.uid=? ORDER BY c.tstamp DESC";
	static final String Delete_Cart_Query = "DELETE FROM Cart WHERE id=?";

	static final PStatementCB2<Cart> Cart_PSCB = new PStatementCB2<Cart>() {

		@Override
		public void call(PreparedStatement pstmt, Cart cart)
				throws SQLException {
			int pos = 0;
			pstmt.setInt(++pos, PageUtil.toInt(cart.getUid()));
			pstmt.setInt(++pos, PageUtil.toInt(cart.getGid()));
			pstmt.setDouble(++pos, cart.getPrice());
			pstmt.setInt(++pos, cart.getQty());
			pstmt.setDate(++pos, cart.getTstamp());
		}
	};

	static final ResultSetCB<List<CartGoods>> CartGoods_RSCB = new ResultSetCB<List<CartGoods>>() {

		@Override
		public List<CartGoods> call(ResultSet rs, int count)
				throws SQLException {
			List<CartGoods> list = new ArrayList<CartGoods>();
			while (rs.next()) {
				CartGoods goods = new CartGoods();
				goods.setCid(rs.getLong("cid"));
				goods.setId(rs.getLong("id"));
				goods.setName(rs.getString("name"));
				goods.setDesc(rs.getString("desc"));
				goods.setPrice(rs.getDouble("price"));
				goods.setTstamp(rs.getDate("tstamp"));
				goods.setCategory(rs.getShort("category"));
				goods.setBarcode(rs.getString("barcode"));
				list.add(goods);
			}
			return list;
		}
	};

	@Override
	public Cart create(Cart entity) throws ApplicationException {
		try {
			int count = dbService.executeUpdate(Create_Cart_Query, Cart_PSCB,
					entity);
			if (count > 0)
				return entity;
			return null;
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}

	@Override
	public Cart retrive(long id) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Cart update(Cart entity) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Cart delete(Cart entity) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<CartGoods> list(Long uid) throws ApplicationException {
		try {
			return dbService.executeQuery(List_Cart_Query,
					new Object[] { PageUtil.toInt(uid) },
					new int[] { Types.INTEGER }, CartGoods_RSCB);
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}

	public boolean delete(Long cid) throws ApplicationException {
		try {
			int count = dbService.executeUpdate(Delete_Cart_Query,
					new Object[] { PageUtil.toInt(cid) },
					new int[] { Types.INTEGER });
			return count > 0;
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}
}
